home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / STANDALO / F1-F4 / F1-F4.C next >
Text File  |  1990-05-09  |  3KB  |  130 lines

  1. /*
  2.     "F1-F4.c"
  3.  
  4.     INIT that patches _PostEvent so that the function keys F1 to F4 on
  5.     the Apple extended keyboard behave as they should in most applications.
  6.     This patch checks to see if the event is a keyDown event. If it is the
  7.     function keys F1, F2, F3 and F4 are translated to command-Z (undo),
  8.     command-X (cut), command-C (copy) and command-V (paste) respectively.
  9.     That's all there is, easy isn't it?
  10.  
  11.     "Set Project Type..." for THINK C 4.02:
  12.         Code resource    on
  13.         File type        INIT
  14.         Creator            ????
  15.         Name            F1-F4
  16.         Type            INIT
  17.         ID                128
  18.         Custom header    checked
  19.         Attrs            00
  20.  
  21.     Erny Tontlinger        version 1.0        09-May-1990
  22. */
  23.  
  24. #define POSTEVENT        0xA02F        /* Trap to patch */
  25. #define JMPopc            0x4EF9        /* Used for self-modifying code */
  26. #define CmdByte            0x174+6        /* KeyMap+6 */
  27. #define CmdBit            7            /* Bit for command key */
  28.  
  29. /* #define CLEANUP */
  30.  
  31. void main (void)
  32. {
  33.     asm {
  34.         ;Called here at INIT time
  35.         ;========================
  36.  
  37.         MOVEM.L    D4-D5,-(SP)
  38.  
  39.         MOVE    #POSTEVENT,D0        ;Save original trap address
  40.         _GetTrapAddress
  41.         LEA        @OrigTrap,A1
  42.         MOVE.L    A0,(A1)
  43.  
  44.         LEA        @Last,A0            ;Move patch into system heap
  45.         LEA        @Patch,A1            ;(It's a shame we cannot use
  46.         SUBA.L    A1,A0                ; MOVE.L #@Last-@Patch,D0)
  47.         MOVE.L    A0,D4                ;D4 = byte count
  48.         MOVE.L    A0,D0
  49.         _NewPtr    SYS
  50.         BNE.S    @NoPatch            ;No memory available
  51.         MOVE.L    A0,D5                ;D5 = destination
  52.  
  53.         MOVE    #POSTEVENT,D0        ;Set our patch
  54.         _SetTrapAddress
  55.  
  56.         LEA        @Patch,A0            ;Move patch into place
  57.         MOVE.L    D5,A1                ;Destination
  58.         MOVE.L    D4,D0                ;Byte count
  59.         _BlockMove
  60.  
  61. @NoPatch
  62.         MOVEM.L    (SP)+,D4-D5            ;Restore registers
  63.         RTS                            ; and return
  64.  
  65.         ;New _PostEvent
  66.         ;==============
  67.         ;On entry:    A0: eventCode (word)
  68.         ;            D0: eventMsg (long word)
  69.         ;On exit:    DO: result code (word)
  70.  
  71. @Patch
  72.         CMPI    #keyDown,A0            ;keyDown event?
  73.         BNE.S    @Return2
  74.         CMPI.B    #0x10,D0            ;Function key?
  75.         BNE.S    @Return2
  76.  
  77.         MOVE.L    A0,-(SP)
  78.         ROR        #8,D0                ;Key code ==> character code
  79.         LEA        @FunctionKeys,A0    ;Check for F1-F4
  80.         BRA.S    @Test
  81. @Loop
  82.         CMP.B    (A0)+,D0
  83.         BNE.S    @Test
  84.  
  85. #ifdef CLEANUP
  86.         LEA        @Exit,A0            ;Save original return address
  87.         MOVE.L    4(SP),(A0)
  88.         LEA        @Tail,A0            ;Set return address to
  89.         MOVE.L    A0,4(SP)            ; our tail patch
  90. #endif
  91.  
  92.         MOVE.L    #CmdByte,A0            ;Set bit in "KeyMap" corresponding
  93.         BSET.B    #CmdBit,(A0)        ; to command key
  94.         BRA.S    @Return1
  95. @Test
  96.         TST.B    (A0)
  97.         BNE.S    @Loop
  98.         ROR        #8,D0                ;Restore original D0
  99. @Return1
  100.         MOVE.L    (SP)+,A0            ;Restore original A0
  101.  
  102. @Return2
  103.         DC        JMPopc                ;Jump to original _PostEvent
  104. @OrigTrap
  105.         DC.L    0
  106.  
  107. #ifdef CLEANUP
  108.         ;_PostEvent returns here
  109.         ;=======================
  110.         ;(This seems not to be necessary)
  111.  
  112. @Tail
  113.         MOVE.L    A0,-(SP)
  114.         MOVE.L    #CmdByte,A0            ;Clear bit in "KeyMap" corresponding
  115.         BCLR.B    #CmdBit,(A0)        ; to command key
  116.         MOVE.L    (SP)+,A0
  117.         DC        JMPopc                ;Return to original caller
  118. @Exit
  119.         DC.L    0
  120. #endif
  121.  
  122.         ;Data
  123.         ;====
  124.  
  125. @FunctionKeys    DC.B    'z','x','c','v',0    ;undo, cut, copy, paste
  126.  
  127. @Last
  128.     }
  129. }
  130.